package com.example.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        
        // 模拟登录成功
        if (username != null && !username.isEmpty()) {
            HttpSession session = request.getSession();
            session.setAttribute("user", username);
            // 登录成功后，重定向到管理员页面
            response.sendRedirect("admin/dashboard.jsp");
        } else {
            // 登录失败，返回登录页
            response.sendRedirect("login.jsp");
        }
    }
}